#!/usr/bin/env bash
# Summary: List all available commands of goenv
# Usage: goenv commands [--sh|--no-sh]

set -e
[ -n "$GOENV_DEBUG" ] && set -x

# Provide goenv completions
if [ "$1" = "--complete" ]; then
  echo --sh
  echo --no-sh
  exit
fi

if [ "$1" = "--sh" ]; then
  sh=1
  shift
elif [ "$1" = "--no-sh" ]; then
  nosh=1
  shift
fi

shopt -s nullglob

{
  # goenv-init uses --sh, don't output special commands then
  if [ -z "$sh" ]; then
    # Output special commands (plugins and version identifiers)
    echo install
    echo latest
    echo system
    echo uninstall
    goenv-versions --bare
  fi

  for command in $(compgen -c goenv-); do
    [ -x "$(command -v "$command")" ] || continue
    command="${command##goenv-}"
    if [ -n "$sh" ]; then
      if [[ ${command:0:3} == "sh-" ]]; then
        echo "${command##sh-}"
      fi
    elif [ -n "$nosh" ]; then
      if [[ ${command:0:3} != "sh-" ]]; then
        echo "${command##sh-}"
      fi
    else
      echo "${command##sh-}"
    fi
  done
} | sort | uniq | grep -v -E '^(echo|--version|realpath\.dylib)$'
